#GLOBAL
struct VSOutPSIn
{
	float4 Position_VSPS : SV_POSITION0;
	float2 Texcoord_VSPS : TEXCOORD0;
};
float simulateSize;
float2 sampleOffsets[8];
sampler2D MyTexture;
#END

#VS
struct VSIn
{
	float3 Position_VS : POSITION0;
	float2 Texcoord_VS : TEXCOORD0;
};


VSOutPSIn main(VSIn In)
{
	VSOutPSIn Out;

	Out.Position_VSPS = float4(In.Position_VS, 1);


	float2 uv = float2(In.Texcoord_VS.x, 1.0-In.Texcoord_VS.y);



	Out.Texcoord_VSPS = uv + (simulateSize * .5);






	return Out;
}
#END

#PS

struct PSOut
{
	float4 Color_PS : SV_TARGET0;
};

PSOut main(VSOutPSIn In)
{
	PSOut Out;

	float4 color = tex2D(MyTexture, In.Texcoord_VSPS);
	color = (color-0.5) * 2.0;

	float3 normal = 0;
	for (int i = 0; i != 8; ++i)
	{
		float2 offset = sampleOffsets[i];
		float4 block = tex2D(MyTexture, In.Texcoord_VSPS + (offset * simulateSize));
		block = (block-0.5) * 2.0;

		float3 vec = float3(offset, block.z-color.z);
		float3 up = float3(0, 0, 1);

		up = cross(vec, up);
		normal += cross(vec, up);
	}
	normal = -normalize(normal);
	Out.Color_PS = float4((normal+1.0) * 0.5, 1);

	return Out;
}
#END

